import { TOKEN_NAME } from "@/config";
import getParams from "@/utils/getParams";
import { getStorage, isEmptyObj } from "@/utils/utils";
import { Button, Result } from "antd";
import { useMemo, useState } from "react";
import { useNavigate } from "react-router-dom";

const Empty = () => {
  const navigate = useNavigate();
  const goHome = () => {
    if (getStorage(TOKEN_NAME)) {
      navigate("/", { replace: true });
    } else {
      navigate("/login", { replace: true });
    }
  };
  const [status] = useState(
    isEmptyObj(getParams("code")) ? 404 : getParams("code")
  );

  const content = useMemo(() => {
    if (status == 403) {
      return "您没有权限访问此页面";
    } else if (status == 500) {
      return "接口出现异常,请联系管理员";
    } else {
      return "没有找到此页面";
    }
  }, [status]);

  return (
    <Result
      status={status}
      title={status}
      subTitle={content}
      extra={
        <Button type="primary" onClick={goHome}>
          返回{getStorage(TOKEN_NAME) ? "首页" : "登录页"}
        </Button>
      }
    />
  );
};

export default Empty;
